<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.5.0/axios.js"></script>

</head>

<body>
    用户名：<input type="text" name="" id=""> <br>
    密码：<input type="text" name="" id=""><br>
    确认密码：<input type="text" name="" id=""><br>
    昵称：<input type="text" name="" id=""><br>
    <input onclick="regFn()" type="button" name="" id="" value="注册"><br>
    <script>
        let baseURL = 'http://localhost:8888';
        let http = axios.create({
            baseURL,
            headers: {
                'content-type': 'application/x-www-form-urlencoded'
            }
        });
        /*
        
            注册
                点击注册按钮的时候 ，
                    获取 注册接口需要的参数(参见接口文档 4.3.1)
                    发起注册请求


        */

        //    let promise =  http.get();
        //    promise.then(r=>{

        //    }).catch(err=>{

        //    })

        // async function box() {
        //     try {
        //         // let r = await promise对象
        //         let r = await http.get()
        //         console.log(r);

        //     } catch (error) {


        //     }
        // }



        // 行内事件-给注册按钮添加点击事件
        async function regFn() {
            // 获取 注册接口需要的参数(参见接口文档 4.3.1)
            let ipts = document.querySelectorAll('input');
            let username = ipts[0].value;
            let password = ipts[1].value;
            let rpassword = ipts[2].value;
            let nickname = ipts[3].value;






            // 发起注册请求
            // 
            let regAPI = '/users/register';
            // 参数在请求体中
            let r = await http.post(regAPI, { username, password, rpassword, nickname });
            console.log(r.data);
            // 解构赋值
            let { code, message } = r.data;

            alert(message);
            if (code == 1) {
                //注册成功- 
                // 自动跳转到登录
                location.href = './15-登录.html'
            }



        }


    </script>


</body>

</html>